package com.dnake.utils;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.dnake.Constant;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by haoyuliu on 2017/1/3.
 */

public class FileUtils {

    /**
     * 删除指定目录下文件及目录
     * @param deleteThisPath
     * @param filePath
     * @return
     */
    public static void deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!TextUtils.isEmpty(filePath)) {
            try {
                File file = new File(filePath);
                if (file.isDirectory()) {// 处理目录
                    File files[] = file.listFiles();
                    for (int i = 0; i < files.length; i++) {
                        deleteFolderFile(files[i].getAbsolutePath(), true);
                    }
                }
                if (deleteThisPath) {
                    if (!file.isDirectory()) {// 如果是文件，删除
                        file.delete();
                        Log.e("deleteFolderFile", "deleteFolderFile: 成功" );
                    } else {// 目录
                        if (file.listFiles().length == 0) {// 目录下没有文件或者目录，删除
                            file.delete();
                        }
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static File getSDRootFile() {
        return Environment.getExternalStorageDirectory();
    }
    public static File getSnapshotDirectory() {
        File sdRootFile = getSDRootFile();
        File file = null;
        if (sdRootFile != null && sdRootFile.exists()) {
            file = new File(sdRootFile.getAbsolutePath()+"/snapshot");
            if (!file.exists()) {
                boolean success = file.mkdirs();
            }
        }

        return file;
    }


    public static File getDir() {
        File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        return sdDir;
    }


    public static boolean saveSnapshotBitmap(Bitmap bitmap, String imgName,int quality) {
        try {
            String dir = getSnapshotDirectory().getAbsolutePath();  //图片保存的文件夹名
            File file = new File(dir);                                 //已File来构建
            if (!file.exists()) {                                     //如果不存在  就mkdirs()创建此文件夹
                file.mkdirs();
            }
            Log.i("SaveImg", "file uri==>" + dir);
            File mFile = new File(dir + "/"+imgName);                        //将要保存的图片文件
            if (mFile.exists()) {
                return false;
            }
            Logger.e(mFile.getAbsolutePath());
            FileOutputStream outputStream = new FileOutputStream(mFile);     //构建输出流
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);  //compress到输出outputStream
            return true;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 保存bitmap 到本地
     * @param bm
     * @param picName
     */
    public static File saveBitmap(Bitmap bm, String picName) {
        File pictureFileDir = getDir();
        if (!pictureFileDir.exists()) {
            pictureFileDir.mkdir();
        }
        File f = new File(Constant.FILE_DIR,picName);
        if (f.exists()) {
            final File to = new File(f.getAbsolutePath() + System.currentTimeMillis());
            f.renameTo(to);
            f.delete();
        }
        Matrix matrix = new Matrix();
        matrix.postScale(-1, 1); // 镜像水平翻转
//        matrix.postRotate(180);
        Bitmap nbmp2 = Bitmap.createBitmap(bm, 0,0, bm.getWidth(),  bm.getHeight(), matrix, true);
        try {
            FileOutputStream out = new FileOutputStream(f);
            nbmp2.compress(Bitmap.CompressFormat.JPEG, 50, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bm!=null){
                bm.recycle();
            }
            if (nbmp2!=null){
                nbmp2.recycle();
            }
        }
        return f;
    }
}
